package com.stylefeng.guns.modular.hhtb.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.mongodb.BasicDBObject;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.modular.hhtb.mongodb.CatCoinLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/mongodb")
public class MongodbController extends BaseController {
    private String PREFIX = "/system/log/";
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 跳转到导航管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "interface_log.html";
    }
    @RequestMapping(value = "/getName")
    @ResponseBody
    public Object getName(){
        String name = mongoTemplate.getDb().getName();
        return name;
    }
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object getLog(CatCoinLog catCoinLog) throws Exception{
        Criteria criteria=Criteria.where("logtype").ne("").ne(null);
        if (!"".equals(catCoinLog.getLogTypeId())&&catCoinLog!=null ){
            criteria.and("logTypeId").is(catCoinLog.getLogTypeId());
        }
        if (!"".equals(catCoinLog.getLogname()) && catCoinLog.getLogname()!=null ){
            criteria.and("logname").is(catCoinLog.getLogname());
        }
        if (!"".equals(catCoinLog.getBeginTime()) && catCoinLog.getBeginTime()!=null
                && !"".equals(catCoinLog.getEndTime()) && catCoinLog.getEndTime()!=null ){
            criteria.and("createtime").gte(catCoinLog.getBeginTime()).lte(catCoinLog.getEndTime());
        }else {
            if (!"".equals(catCoinLog.getBeginTime()) && catCoinLog.getBeginTime()!=null ){
                criteria.and("createtime").gte(catCoinLog.getBeginTime());
            }
            if (!"".equals(catCoinLog.getEndTime()) && catCoinLog.getEndTime()!=null ){
                criteria.and("createtime").lte(catCoinLog.getEndTime());
            }
        }

        Query query=new Query(criteria);
        query.with(new Sort(Sort.Direction.DESC,"createtime"));
        HttpServletRequest request = HttpKit.getRequest();
        int limit = Integer.valueOf(request.getParameter("limit"));     //每页多少条数据
        int offset = Integer.valueOf(request.getParameter("offset"));   //每页的偏移量(本页当前有多少条)
        query.skip(offset);//跳过多少条
        query.limit(limit);
        Page<CatCoinLog> page = new PageFactory<CatCoinLog>().defaultPage();
        List<CatCoinLog> logs = mongoTemplate.find(query,CatCoinLog.class);

        Long count = mongoTemplate.count(query, CatCoinLog.class);
        page.setRecords(logs);
        page.setTotal(Integer.parseInt(count.toString()));
        return super.packForBT(page);
    }

    @RequestMapping("/delLog")
    @ResponseBody
    public  void delLog(){
        Query query=new Query();
        mongoTemplate.remove(query,CatCoinLog.class);
    }
}
